﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using PBOffice.Models;

public partial class SystemManager_RoleManager : System.Web.UI.Page
{
    List<RoleInfo> list = new RoleInfoManager().GetAllRoleInfo();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.btnModify.Enabled = false;
            this.GridView1.DataSource = list;
            this.GridView1.DataBind();
        }
    }
    private static int roleId;
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        roleId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        this.txtRoleName.Text = GridView1.Rows[e.RowIndex].Cells[0].Text.Trim();
        this.txtRoleDesc.Text = GridView1.Rows[e.RowIndex].Cells[1].Text.Trim();
        this.btnModify.Enabled = true;


    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

        int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
        new RoleInfoManager().DelteRoleInfo(Id);
        this.GridView1.DataSource = new RoleInfoManager().GetAllRoleInfo();
        this.GridView1.DataBind();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (CheckEmpty() == true)
        {
            RoleInfo roleInfo = new RoleInfo();
            roleInfo.RoleName = this.txtRoleName.Text.Trim();
            roleInfo.RoleDesc = this.txtRoleDesc.Text.Trim();
            new RoleInfoManager().AddRoleInfo(roleInfo);
            this.GridView1.DataSource = new RoleInfoManager().GetAllRoleInfo();
            this.GridView1.DataBind();
        }
        this.txtRoleName.Text = "";
        this.txtRoleDesc.Text = "";
    }
    protected void btnModify_Click(object sender, EventArgs e)
    {
        RoleInfo roleInfo = new RoleInfo();
        roleInfo.RoleId = roleId;
        roleInfo.RoleName = this.txtRoleName.Text.Trim();
        roleInfo.RoleDesc = this.txtRoleDesc.Text.Trim();
        new RoleInfoManager().UpdateRoleInfo(roleInfo);
        this.GridView1.DataSource = new RoleInfoManager().GetAllRoleInfo();
        this.GridView1.DataBind();
        this.txtRoleName.Text = "";
        this.txtRoleDesc.Text = "";
        this.btnModify.Enabled = false;

    }
    private bool CheckEmpty()
    {
        if (this.txtRoleName.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('角色名称不能为空！')</script>");
            return false;
        } if (this.txtRoleDesc.Text == "")
        {
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "alert", "<script>alert('角色说明不能为空！')</script>");
            return false;
        }
        return true;
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "right")
        {
            GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
            int id = Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value);
            Response.Redirect(string.Format("RoleRight.aspx?id={0}", id));
            // int id1 = Convert.ToInt32(GridView1.Rows[drv.RowIndex].Cells[0].Text);  
        }
        if (e.CommandName == "stf")
        {
            GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent));
            int id = Convert.ToInt32(GridView1.DataKeys[drv.RowIndex].Value);
            Response.Redirect(string.Format("RoleUser.aspx?id={0}", id));
            //int id = Convert.ToInt32(e.CommandArgument.ToString());
        }

    }
}